package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.IEmployArtistTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/03.
 * 雇佣艺术家发布任务 控制层
 */
@Controller
@RequestMapping("/employArtistTask")
public class EmployArtistTaskController {

    @Autowired
    private IEmployArtistTaskService employArtistTaskService;


    /**
     * 跳转到雇佣艺术家页面的页面
     * @return
     */
    @RequestMapping(value = "/employArtistTask", method = RequestMethod.GET)
    public String employArtistTask(int artist_id,HttpServletRequest request) {

        Map<String,Object> map = null;
        try {
            map = employArtistTaskService.findInitData(artist_id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("initDataMap",map);


        return "/employArtist/employArtistTaskInfo";
    }

    /**
     * 跳转到确认页面
     * @return
     */
    @RequestMapping(value = "/confirmTaskInfo", method = RequestMethod.GET)
    public String confirmTaskInfo(String taskId,HttpServletRequest request) {

        Map<String,Object> map = null;
        try {
            //
             map =  employArtistTaskService.updateFindTaskInfoByTaskId(taskId);

        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("taskInfoMap",map);
        request.setAttribute("taskId",taskId);

        return "/employArtist/confirmTaskInfo";
    }




    /**
     * 查询所有初始数据（需求技能、需求风格、增值服务）
     * @return
     */
    @RequestMapping(value = "/findInitData", method = RequestMethod.POST)
    public ResponseEntity<?> findInitData(int userId) {



        try {
            Map<String,Object> map =  employArtistTaskService.findInitData(userId);

            ResponseEntity<Map<String,Object>> responseEntity = new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }






    /**
     * 新增任务信息和竞标信息
     * @param param 需求数据
     * @return
     */
    @RequestMapping(value = "/insertTaskInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?>  insertTaskInfo(@RequestParam Map param,HttpServletRequest request) {



        try {
            //获取当前用户ID
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
            param.put("user_id",userInfo.getUserId().toString());


            String language = "zh_cn";
            //从session中取出当前语言环境
            if(request.getSession().getAttribute("language") != null){

                language = request.getSession().getAttribute("language").toString();
            }


            param.put("lang",language);

            if(param.get("item_id") == null || "".equals(param.get("item_id"))){

                //新增任务
                employArtistTaskService.addTaskInfoAndBidinfo(param);
            }else{

                employArtistTaskService.updateTaskInfoById(param);
            }




            ResponseEntity<String> responseEntity = new ResponseEntity<String>(param.get("item_id").toString(), HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("error", HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }





}
